/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "DictOptions.h"
#include "DictManager.h"
#include "Logger.h"

DictOptions::DictOptions() :
	mLookupMode(LM_Normal),
	mLookupDelayMS(500),
	mShowWordList(true),
	mShowWordNav(true),
	mMainShadingColor("#FFFFFF"),
	mSelectShadingColor("#FFFFFF"),
	thesaurusDictFileName("mwt"),
#ifdef Q_OS_WIN32
	useExternalPlayer(false)
#else
	useExternalPlayer(true),
	playSoundCommand("mplayer")
#endif
{
	readSettings();
	DM.setKeyMap(mQueryDictList);
}

DictOptions::~DictOptions()
{
	writeSettings();
}

void DictOptions::readSettings()
{
	QSettings settings("./configure/Dictionary.conf" , QSettings::IniFormat );
	settings.beginGroup( "Global" );
	QVariant val;

	val = settings.value("LookupMode");
	if(val.isValid()) mLookupMode = LookupMode(val.toInt());

	val = settings.value("LookupDelayMS");
	if(val.isValid()) mLookupDelayMS = val.toInt();

	val = settings.value("ShowWordList");
	if(val.isValid()) mShowWordList = val.toBool();

	val = settings.value("ShowWordNav");
	if(val.isValid()) mShowWordNav = val.toBool();

	val = settings.value("MainShadingColor");
	if(val.isValid()) mMainShadingColor = val.toString();

	val = settings.value("SelectShadingColor");
	if(val.isValid()) mSelectShadingColor = val.toString();

	val = settings.value("ThesaurusDictFileName");
	if(val.isValid()) thesaurusDictFileName = val.toString();

	val = settings.value("QueryDicts");
	if(val.isValid()) mQueryDictList = val.toString().split("|", QString::SkipEmptyParts);
	for (int i=0; i<mQueryDictList.size(); i++)
	{
		if (!DM.getAvailableDictList().contains(mQueryDictList[i]))
			mQueryDictList.removeAt(i);
	}
	//LOG<<"mQueryDictList.size()="<<mQueryDictList.size()<<endl;

	val = settings.value("UseQSound");
	if(val.isValid()) useExternalPlayer = val.toBool();

	val = settings.value("PlaySoundCommand");
	if(val.isValid()) playSoundCommand = val.toString();

	val = settings.value("SpeakLibPath");
	if(val.isValid()) speakLibPath = val.toString();

	settings.endGroup();
}

void DictOptions::writeSettings()
{
	QSettings settings("./configure/Dictionary.conf" , QSettings::IniFormat );
	settings.beginGroup( "Global" );
	QVariant val;

	settings.setValue("LookupMode", mLookupMode);
	settings.setValue("LookupDelayMS", mLookupDelayMS);
	settings.setValue("ShowWordList", mShowWordList);
	settings.setValue("ShowWordNav", mShowWordNav);

	settings.setValue("MainShadingColor", mMainShadingColor.name());
	settings.setValue("SelectShadingColor", mSelectShadingColor.name());

	settings.setValue("QueryDicts", mQueryDictList.join("|"));
	settings.setValue("ThesaurusDictFileName", thesaurusDictFileName);

	settings.setValue("UseQSound", useExternalPlayer);
	settings.setValue("PlaySoundCommand", playSoundCommand);
	settings.setValue("SpeakLibPath", speakLibPath);

	settings.endGroup();
}

